home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Offline Browsing / HTTrack.exe / data1.cab / Sources / src / htstools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-28  |  10.4 KB  |  381 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       various tools (filename analyzing ..)                  */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #include "htstools.h"
  39.  
  40. /* specific definitions */
  41. #include "htsbase.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. /* END specific definitions */
  47.  
  48.  
  49. // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
  50. // [adr et fil sont des buffers de 1ko]
  51. // 0 : ok
  52. // -1 : erreur
  53. // -2 : protocole non supportΘ (ftp)
  54. int ident_url_relatif(char *lien,char* origin_adr,char* origin_fil,char* adr,char* fil) {
  55.   int ok=0;
  56.  
  57.   adr[0]='\0'; fil[0]='\0';    //effacer buffers
  58.  
  59.   // lien non vide!
  60.   if (strnotempty(lien)==0) return -1;    // erreur!
  61.  
  62.   // filtrer les parazites (mailto & cie)
  63.   if (strfield(lien,"mailto:")) {  // ne pas traiter
  64.     ok=-1;
  65.   }
  66.   else if (strfield(lien,"news:")) {  // ne pas traiter
  67.     ok=-1;
  68.   }
  69.   //else if (strfield(lien,"file:")) {  // ne pas traiter
  70.   //  ok=-1;
  71.   //}
  72.   else if (strfield(lien,"javascript:")) {  // ne pas traiter
  73.     ok=-1;
  74.   }
  75.   else if (strstr(lien,":/")) {    // c'est une URL?
  76.     if (strfield(lien,"http:")) {
  77.       if (ident_url(lien,adr,fil)==-1) {        
  78.         ok=-1;    // erreur URL
  79.       }
  80.     } else if (strfield(lien,"file:")) {
  81.       if (ident_url(lien,adr,fil)==-1) {        
  82.         ok=-1;    // erreur URL
  83.       }
  84.     } else if (strfield(lien,"ftp:")) {
  85.       if (ftp_available()) {     // ftp supportΘ
  86.         if (ident_url(lien,adr,fil)==-1) {        
  87.           ok=-1;    // erreur URL
  88.         }
  89.       } else {
  90.         ok=-2;  // non supportΘ
  91.       }
  92.     } else {    // je ne connais pas ce protocole!!
  93.       ok=-1;
  94.     }   
  95.   } else {    // c'est un lien relatif
  96.     char* a;
  97.     
  98.     // On forme l'URL complΦte α partie de l'url actuelle
  99.     // et du chemin actuel si besoin est.
  100.     
  101.     // copier adresse
  102.     if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
  103.       strcpy(adr,origin_adr);    // mΩme adresse
  104.  
  105.       /* bogus form: http:relative.html */
  106.       if (strfield(lien,"http:"))
  107.         lien+=5;
  108.  
  109.       if (*lien!='/') {  // sinon c'est un lien absolu
  110.         a=strchr(origin_fil,'?');
  111.         if (!a) a=origin_fil+strlen(origin_fil);
  112.         while((*a!='/') && ( ((int) a) > ((int) origin_fil)) ) a--;
  113.         if (*a=='/') {    // ok on a un '/'
  114.           if ( (((int) a)-((int) origin_fil)+1+strlen(lien)) < HTS_URLMAXSIZE) {
  115.             // copier chemin
  116.             strncpy(fil,origin_fil,((int) a)-((int) origin_fil)+1);
  117.             *(fil + ((int) a)-((int) origin_fil)+1)='\0';
  118.             
  119.             // copier chemin relatif
  120.             if (((int) strlen(fil)+(int) strlen(lien))<HTS_URLMAXSIZE) {
  121.               strcat(fil,lien + ((*lien=='/')?1:0) );      
  122.               // simplifier url pour les ../
  123.               fil_simplifie(fil);
  124.             } else
  125.               ok=-1;    // erreur
  126.           } else {    // erreur
  127.             ok=-1;    // erreur URL
  128.           }
  129.         } else {    // erreur
  130.           ok=-1;    // erreur URL
  131.         }
  132.       } else { // chemin absolu
  133.         // copier chemin directement
  134.         strcat(fil,lien);      
  135.       }  // *lien!='/'
  136.     } else
  137.       ok=-1;
  138.     
  139.   }  // test news: etc.
  140.  
  141.   // case insensitive pour adresse
  142.   {
  143.     char *a=jump_identification(adr);
  144.     while(*a) {
  145.       if ((*a>='A') && (*a<='Z'))
  146.         *a+='a'-'A';       
  147.       a++;
  148.     }
  149.   }
  150.   
  151.   return ok;
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158. // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
  159. // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
  160. int lienrelatif(char* s,char* link,char* curr_fil) {
  161.   char _curr[HTS_URLMAXSIZE*2];
  162.   char newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
  163.   char* curr;
  164.   //int n=0;
  165.   char* a;
  166.   int slash=0;
  167.   //
  168.   newcurr_fil[0]='\0'; newlink[0]='\0';
  169.   //
  170.  
  171.   // patch: Θliminer les ? (paramΦtres) sinon bug
  172.   if ( (a=strchr(curr_fil,'?')) ) {
  173.     strncat(newcurr_fil,curr_fil,(int) a-(int) curr_fil);
  174.     curr_fil = newcurr_fil;
  175.   }
  176.   if ( (a=strchr(link,'?')) ) {
  177.     strncat(newlink,link,(int) a-(int) link);
  178.     link = newlink;
  179.   }
  180.  
  181.   // recopier uniquement le chemin courant
  182.   curr=_curr;
  183.   strcpy(curr,curr_fil);
  184.   if ((a=strchr(curr,'?'))==NULL)  // couper au ? (params)
  185.     a=curr+strlen(curr)-1;         // pas de params: aller α la fin
  186.   while((*a!='/') && ((int) a>(int) curr)) a--;       // chercher dernier / du chemin courant
  187.   if (*a=='/') *(a+1)='\0';                           // couper dernier /
  188.   
  189.   // "effacer" s
  190.   s[0]='\0';
  191.   
  192.   // sauter ce qui est commun aux 2 chemins
  193.   {
  194.     char *l,*c;
  195.     if (*link=='/') link++;  // sauter slash
  196.     if (*curr=='/') curr++;
  197.     l=link;
  198.     c=curr;
  199.     // couper ce qui est commun
  200. #if HTS_CASSE
  201.     while ((*link==*curr) && (*link!=0)) {link++; curr++; }
  202. #else
  203.     while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
  204. #endif
  205.     // mais on veut un rΘpertoirer entier!
  206.     // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
  207.     while(((*link!='/') || (*curr!='/')) && ((int) link>(int) l)) { link--; curr--; }
  208.     //if (*link=='/') link++;
  209.     //if (*curr=='/') curr++;
  210.   }
  211.   
  212.   // calculer la profondeur du rΘpertoire courant et remonter
  213.   // LES ../ ONT ETE SIMPLIFIES
  214.   a=curr;
  215.   if (*a=='/') a++;
  216.   while(*a) if (*(a++)=='/') strcat(s,"../");
  217.   //if (strlen(s)==0) strcat(s,"/");
  218.  
  219.   if (slash) strcat(s,"/");    // garder absolu!!
  220.   
  221.   // on est dans le rΘpertoire de dΘpart, copier
  222.   strcat(s,link + ((*link=='/')?1:0) );
  223.  
  224.   // on a maintenant une chaine de la forme ../../test/truc.html  
  225.   return 0;
  226. }
  227.  
  228.  
  229. // conversion chemin de fichier/dossier vers 8-3
  230. void long_to_83(char* n83,char* save) {
  231.   n83[0]='\0';
  232.  
  233.   while(*save) {
  234.     char fn83[16],fnl[256];
  235.     int i=0;
  236.     while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
  237.     fnl[i]='\0';
  238.     // conversion
  239.     longfile_to_83(fn83,fnl);
  240.     strcat(n83,fn83);
  241.  
  242.     save+=i;
  243.     if (*save=='/') { strcat(n83,"/"); save++; }
  244.   }
  245. }
  246.  
  247.  
  248. // conversion nom de fichier/dossier vers 8-3
  249. void longfile_to_83(char* n83,char* save) {
  250.   int i=0,j=0;
  251.   char nom[8+1]="";
  252.   char ext[3+1]="";
  253.   
  254.   while((i<8) && (save[j]) && (save[j]!='.')) { if (save[j]!=' ') { nom[i]=save[j]; i++; } j++; }  // recopier nom
  255.   nom[i]='\0';
  256.   if (save[j]) {  // il reste au moins un point
  257.     i=strlen(save)-1;
  258.     while((i>0) && (save[i]!='.') && (save[i]!='/')) i--;    // rechercher dernier .
  259.     if (save[i]=='.') {  // point!
  260.       int j=0;
  261.       i++;
  262.       while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
  263.       ext[j]='\0';
  264.     }
  265.   }
  266.   // corriger vers 8-3
  267.   n83[0]='\0';
  268.   strncat(n83,nom,8);
  269.   if (strnotempty(ext)) {
  270.     strcat(n83,".");
  271.     strncat(n83,ext,3);    
  272.   }
  273. }
  274.  
  275. // Θcrire backblue.gif
  276. int verif_backblue(char* base) {
  277.   int ret=0;
  278.   if (fsize(fconcat(base,"backblue.gif")) != HTS_DATA_BACK_GIF_LEN) {
  279.     FILE* fp = filecreate(fconcat(base,"backblue.gif"));
  280.     if (fp) {
  281.       if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
  282.         ret=1;
  283.       fclose(fp);
  284.       usercommand(0,NULL,fconcat(base,"backblue.gif"));
  285.     } else
  286.       ret=1;
  287.     //
  288.     fp = filecreate(fconcat(base,"fade.gif"));
  289.     if (fp) {
  290.       if (fwrite(HTS_DATA_FADE_GIF,HTS_DATA_FADE_GIF_LEN,1,fp) != HTS_DATA_FADE_GIF_LEN)
  291.         ret=1;
  292.       fclose(fp);
  293.       usercommand(0,NULL,fconcat(base,"fade.gif"));
  294.     } else
  295.       ret=1;
  296.   } 
  297.   return ret;
  298. }
  299.  
  300.  
  301. // recherche chaεne de type truc<espaces>=
  302. // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
  303. /* SECTION OPTIMISEE:
  304. #define rech_tageq(adr,s) ( \
  305.   ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
  306.     ( (streql(*adr,*s)) ? \
  307.       (__rech_tageq(adr,s)) \
  308.       : 0 \
  309.     ) \
  310.     : 0\
  311.   )
  312. */
  313. /*
  314. HTS_INLINE int rech_tageq(const char* adr,const char* s) { 
  315.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  316.     if (streql(*adr,*s)) {                           // tester premier octet (optimisation)
  317.       return __rech_tageq(adr,s);
  318.     }
  319.   }
  320.   return 0;
  321. }
  322. */
  323. // DeuxiΦme partie
  324. HTS_INLINE int __rech_tageq(const char* adr,const char* s) { 
  325.   int p;
  326.   p=strfield(adr,s);
  327.   if (p) {
  328.     while(is_space(adr[p])) p++;
  329.     if (adr[p]=='=') {
  330.       return p+1;
  331.     }
  332.   }
  333.   return 0;
  334. }
  335.  
  336. // tag sans =
  337. HTS_INLINE int rech_sampletag(const char* adr,const char* s) { 
  338.   register int p;
  339.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  340.     p=strfield(adr,s);
  341.     if (p) {
  342.       if (!isalnum((unsigned char)adr[p])) {  // <srcbis n'est pas <src
  343.         return 1;
  344.       }
  345.       return 0;
  346.     }
  347.   }
  348.   return 0;
  349. }
  350.  
  351. // teste si le tag contenu dans from est Θgal α "tag"
  352. HTS_INLINE int check_tag(char* from,const char* tag) {
  353.   char* a=from+1;
  354.   int i=0;
  355.   char s[256];
  356.   while(is_space(*a)) a++;
  357.   while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
  358.   s[i++]='\0';
  359.   return (strfield2(s,tag));  // comparer
  360. }
  361.  
  362. // teste si un fichier dΘpasse le quota
  363. int istoobig(LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
  364.   int ok=1;
  365.   if (size>0) {
  366.     if (is_hypertext_mime(type)) {
  367.       if (maxhtml>0) {
  368.         if (size>maxhtml)
  369.           ok=0;
  370.       }
  371.     } else {
  372.       if (maxnhtml>0) {
  373.         if (size>maxnhtml)
  374.           ok=0;
  375.       }
  376.     }
  377.   }
  378.   return (!ok);
  379. }
  380.  
  381.